<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, Ref } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { basicProps } from './props';
  import type { deviceStateDataType } from '../api';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n('dashboard');
  const props = defineProps({
    ...basicProps,
    overviewData: {
      type: Object as PropType<deviceStateDataType>,
      required: true,
    },
  });
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  onMounted(() => {
    console.log('onMounted-charts');
    const data = props.overviewData;
    setOptions({
      tooltip: {
        trigger: 'item',
      },
      legend: {
        orient: 'vertical',
        left: 'right',
      },
      series: [
        {
          name: t('deviceState'),
          type: 'pie',
          radius: '70%',
          data: data.deviceStateNameList?.map((item, index) => {
            return { name: item, value: data.deviceStateCountList?.[index] };
          }),
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    });
  });
</script>
